/*
 * Created on 2005-6-8
 *
 */
package com.yl.cap.analyst.user.service;

import jaoso.framework.dao.AccountDAO;
import jaoso.framework.domain.Account;
import jaoso.framework.exception.GroupExistException;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.yl.cap.analyst.exceptions.GroupConflictException;
import com.yl.cap.analyst.exceptions.UserExistException;
import com.yl.cap.analyst.report.domain.ReportLevel;
import com.yl.cap.analyst.user.dao.UserDAO;
import com.yl.cap.analyst.user.domain.Group;
import com.yl.cap.analyst.user.domain.User;

/**
 * @author ZhangQi
 * 
 */
public class UserServiceImpl implements UserService
{
    /**
     * @return Returns the userDAO.
     */
    public UserDAO getUserDAO()
    {
        return userDAO;
    }

    /**
     * @param userDAO
     *            The userDAO to set.
     */
    public void setUserDAO(UserDAO userDAO)
    {
        this.userDAO = userDAO;
    }

    private UserDAO userDAO;

    private AccountDAO accountDAO;

    /**
     * @return Returns the accountDAO.
     */
    public AccountDAO getAccountDAO()
    {
        return accountDAO;
    }

    /**
     * @param accountDAO
     *            The accountDAO to set.
     */
    public void setAccountDAO(AccountDAO accountDAO)
    {
        this.accountDAO = accountDAO;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yl.cap.analyst.user.service.UserService#getAllViewer()
     */
    public List getAllUser()
    {
        return userDAO.getAllUser();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yl.cap.analyst.user.service.UserService#getAllGroup()
     */
    public List getAllGroup()
    {
        return userDAO.getAllGroup();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yl.cap.analyst.user.service.UserService#createGroup(com.yl.cap.analyst.user.domain.Group)
     */
    public void createGroup(Group group) throws GroupExistException
    {
        if (!userDAO.isGroupExist(group.getName()))
        {
            userDAO.saveEntity(group);
        }
        else
        {
            throw new GroupExistException();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yl.cap.analyst.user.service.UserService#updGroup(java.lang.String,
     *      java.lang.String)
     */
    public void updGroup(Group group) throws GroupExistException
    {
        // if (!userDAO.isGroupExist(newName))
        // {
        Group newGroup = (Group) userDAO.loadEntity(Group.class, group.getId());
        // newGroup.setName(newName);
        newGroup.setPopedomField(group.getPopedomField());
        // }
        /*
         * else { throw new GroupExistException(); }
         */
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yl.cap.analyst.user.service.UserService#delGroup(java.lang.String)
     */
    public void delGroup(String id)
    {
        Group group = new Group();
        group.setId(id);
        userDAO.removeEntity(group);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yl.cap.analyst.user.service.UserService#createUser(com.yl.cap.analyst.user.domain.User)
     */
    public void createUser(User user) throws UserExistException,GroupConflictException
    {

        if (userDAO.isUserExist(user.getAccount().getName()))
        {
            throw new UserExistException();
        }
        Set oldGroups = user.getGroups();
        Set groups = new HashSet();
        Group g = null;
        StringBuffer level = new StringBuffer();
        List levels = null;
        for (Iterator i = oldGroups.iterator(); i.hasNext();)
        {
            g = (Group) userDAO.loadEntity(Group.class, (String) i.next());
            levels = g.getReportLevel();
            for (int j = 0; j < levels.size(); j++)
            {
                if (level.indexOf(((ReportLevel) levels.get(j)).getId()) == -1)
                {
                    level.append(((ReportLevel) levels.get(j)).getId()+",");
                }
                else
                {
                    throw new GroupConflictException();
                }
            }
            g.getUsers().add(user);
            groups.add(g);
            userDAO.updateEntity(g);
        }
        user.setGroups(groups);
        userDAO.saveEntity(user);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yl.cap.analyst.user.service.UserService#delUser(java.lang.String[])
     */
    public void delUser(String[] ids)
    {
        /*
         * for (int i = 0; i < ids.length; i++) { User u = (User)
         * userDAO.loadEntity(Account.class,ids[i]); userDAO.removeEntity(u); }
         */
        accountDAO.removeAccount(ids);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yl.cap.analyst.user.service.UserService#getUserById(java.lang.String)
     */
    public Account getUserById(String id)
    {
        return (Account) userDAO.loadEntity(Account.class, id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yl.cap.analyst.user.service.UserService#delGroups(java.lang.String[])
     */
    public void delGroups(String[] ids) throws Exception
    {
        for (int i = 0; i < ids.length; i++)
        {
            Group group = (Group) userDAO.loadEntity(Group.class, ids[i]);

            if (group.getUsers() != null && group.getUsers().size() != 0)
            {
                throw new UserExistException();
            }
            userDAO.removeEntity(group);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yl.cap.analyst.user.service.UserService#getGroupById(java.lang.String)
     */
    public Group getGroupById(String id)
    {
        return (Group) userDAO.loadEntity(Group.class, id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yl.cap.analyst.user.service.UserService#updAccount(jaoso.framework.domain.Account)
     */
    public void updAccount(Account a)
    {
        Account acc = (Account) userDAO.loadEntity(a.getClass(), a.getId());
        acc.setPassword(a.getPassword());
    }
}
